############################################
# Map of Counties with and without Data
############################################

# Set up necessary packages and path to data
library(tidyverse)
library(dplyr)
library(ggplot2)
require(maps)
library(RColorBrewer)
library(haven)

# Set the path to the working directory
path = "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

# Get a list of counties
county_types = read_dta(paste0(path, "/analysis_data/county_demographics.dta")) %>%
  select(state, county) %>%
  distinct() %>%

  # Add in the list of counties that are out of scope
  left_join(read_dta(paste0(path, "/analysis_data/counties_not_in_scope.dta")), by=c("state", "county"))  %>%

  # Add in the list of counties with at least some election data
  left_join(read_dta(paste0(path, "/analysis_data/county_map_data.dta")), by=c("state", "county")) %>%

  # Add in long state names
  left_join(read_dta(paste0(path, "/analysis_data/state_abbrev.dta")), by=c("state")) %>%

  # Prepare the state and county names for merging with the county map data identifiers
  mutate(region = str_to_lower(state_long),
    subregion = str_to_lower(county),
    subregion = case_when(
      county=="O'BRIEN" & state=="IA" ~ "obrien",
      county=="DEKALB" & state=="IL" ~ "de kalb",
      county=="DEWITT" & state=="IL" ~ "de witt",
      county=="DUPAGE" & state=="IL" ~ "du page",
      county=="LASALLE" & state=="IL" ~ "la salle",
      county=="LA GRANGE" & state=="IN" ~ "lagrange",
      county=="LASALLE" & state=="LA" ~ "la salle",
      county=="DEKALB" & state=="MO" ~ "de kalb",
      county=="OGLALA LAKOTA" & state=="SD" ~ "shannon",
      TRUE ~ subregion)) %>%

  # Flag counties as in sample, not in sample, or not in scope
  mutate(county_type = case_when(
    not_in_scope==1 ~ "Not in Scope",
    is.na(races) & is.na(not_in_scope) ~ "Not in Sample",
    !is.na(races) ~ "In Sample")) %>%
  mutate(county_type_alt = case_when(
    not_in_scope==1 ~ "No Partisan Elected LEO",
    is.na(races) & is.na(not_in_scope) ~ "Not in Sample",
    !is.na(races) ~ "In Sample")) %>%

  # Remove Alaska and Hawaii and remove the unnecessary variables
  filter(state!="AK", state!="HI") %>%
  select(region, subregion, county_type, county_type_alt)

# Set up the county boundaries with county type data
map_counties = map_data('county') %>%
  left_join(mutate(county_types), by=c("region", "subregion")) %>%
  mutate(county_type = case_when(
    is.na(county_type) ~ "Not in Scope",
    TRUE ~ county_type)) %>%
  mutate(county_type_alt = case_when(
    is.na(county_type_alt) ~ "No Partisan Elected LEO",
    TRUE ~ county_type_alt))

# Set up the state boundaries
map_states = map_data('state') %>%
  filter(!(region %in% c("alaska", "hawaii")))

# Define the colors for the map
colors_base = brewer.pal(5, 'Blues')
map_colors = colors_base
map_colors[1] = colors_base[2]
map_colors[2] = colors_base[3]
map_colors[3] = '#616161'

# Make and save the map
map = ggplot() +
  geom_polygon(data=map_counties, aes(x = long, y = lat, 
    group = group, fill = county_type),
    color = '#616161', size = .1) +
  geom_polygon(data=map_states, aes(x = long, y = lat, group=group),
    fill=NA, color = '#444444', size = .3) + 
  coord_map("albers", lat0=39, lat1=45) +
  theme_void() + 
  guides(fill = guide_legend(title = '', title.position = 'top')) +
  scale_fill_manual(values = map_colors) +
  theme(legend.position = 'bottom',
        legend.title = element_text(color = 'black', size = 25),
        legend.text = element_text(size = 25),
        legend.key.size = unit(1.15, 'cm'))
ggsave(paste0(path, "/output/county_elec_data_map.pdf"), map, width=15, height=9)

